home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.07 Jul 94 / FoxProAndAppleScript next >
Encoding:
Text File  |  1994-06-16  |  6.3 KB  |  280 lines  |  [TEXT/MSWD]

  1.  
  2.  
  3. tell application “Microsoft FoxPro”
  4.   DO SCRIPT “WAIT WINDOW ¬
  5.     ‘This is my first script’ TIMEOUT 2”
  6. end tell
  7.  
  8. ________________________
  9.  
  10. -- Sample AppleScript showing Required commands.
  11. run application "Microsoft FoxPro"
  12. tell application "Microsoft FoxPro"
  13.   activate "Microsoft FoxPro"
  14.   open "config.fpm"
  15.   Do Script "=INKEY(2)"
  16.   activate "Finder"
  17.   Quit
  18. end tell
  19.  
  20. _______________________
  21.  
  22. * IS_ASCRIPT.PRG
  23.  
  24. #DEFINE C_ASFILE "AppleScript™"
  25. PRIVATE hasascript,asfilename
  26. PRIVATE saveerr,savelib,haderr
  27.  
  28. m.haderr = .F.
  29. m.hasascript = .T.
  30. m.asfilename = ""
  31.  
  32. m.savelib = SET('LIBRARY')
  33. IF ATC('FOXTOOLS',m.savelib)=0
  34.   m.saveerr = ON('ERROR')
  35.   ON ERROR m.haderr = .T.
  36.   SET LIBRARY TO FOXTOOLS ADDITIVE
  37.   ON ERROR &saveerr
  38.   IF m.haderr 
  39.     WAIT WINDOW ;
  40.       "Could not load FoxTools library"
  41.     RETURN .F.
  42.   ENDIF
  43. ENDIF
  44.  
  45. m.asfilename =fxsystem(1)+':'+C_ASFILE
  46. m.hasascript = FILE(m.asfilename)
  47.  
  48. IF ATC('FOXTOOLS',m.savelib)=0
  49.   RELEASE LIBRARY FOXTOOLS
  50. ENDIF
  51.  
  52. RETURN m.hasascript
  53.  
  54. ___________________
  55.  
  56. * Mac Sales program
  57.  
  58. m.filepath = SET("DEFAULT") +;
  59.   "MACHETE:APPLESCRIPT:"
  60. m.salesdbf = m.filepath + "MAC SALES.DBF"
  61. m.xlsheet  = m.filepath + "XL SALES.XLS"
  62. m.xlscript = m.filepath + "XL SCRIPT"
  63.  
  64. SET SAFETY OFF
  65. CLOSE DATA
  66.  
  67. USE (m.salesdbf) ALIAS macsales AGAIN
  68.  
  69. SELECT product,unitprice*quantity AS sales,;
  70.   YEAR(date) AS Year FROM macsales ;
  71.   GROUP BY year,product ORDER BY year;
  72.   INTO CURSOR xlcurs
  73.  
  74. COPY TO (m.xlsheet) TYPE XLS
  75.  
  76. RUNSCRIPT (m.xlscript) TO m.retval
  77.  
  78. __________________
  79.  
  80. -- XL Script is file name 
  81. tell application "Microsoft Excel"
  82.    
  83.   -- setup AppleScript variables
  84.   set x to path to startup disk
  85.   copy (x as string) ¬
  86.     & "Machete:AppleScript:" ¬
  87.     to foxpath
  88.   copy "XL Macros" to xlmacs
  89.   -- FoxPro query file
  90.   copy "XL SALES.XLS" to xlsales 
  91.    
  92.   -- open files if not already open
  93.   if not (Window xlmacs exists) then
  94.     open foxpath & xlmacs
  95.   end if
  96.   if not (Window xlsales exists) then
  97.     open foxpath & xlsales
  98.   end if
  99.    
  100.   -- make sure xlsales document is on top
  101.   set the selection of Document ¬
  102.     xlsales to "R1C1"
  103.    
  104.   -- run excel macro to create cross tab
  105.   Evaluate "run('" & xlmacs & "'!Cross_Tab)"
  106.    
  107.   -- close files, see if Macro file is hidden 1st
  108.   close Window xlsales saving no
  109.   if visible of Window xlmacs = false then
  110.     set visible of Window xlmacs to true
  111.   end if
  112.   close Window xlmacs saving no
  113.    
  114.   --bring Excel to front
  115.   activate "Microsoft Excel"
  116.    
  117. end tell
  118.  
  119. ______________________
  120.  
  121. cross tab
  122. =ACTIVATE("XL SALES.XLS")
  123. =SELECT("C1:C3")
  124. =SET.DATABASE()
  125. =CROSSTAB.CREATE({"product",0,"Auto","Auto","NNNNNNN"},{"year",0,"Auto","Auto","NNNNNNN"},{"sales","SUM(sales)","YNNNN",FALSE},TRUE,TRUE,1,TRUE,TRUE)
  126. =SELECT("crosstab_range")
  127. =FORMAT.AUTO(13,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE)
  128. =APPLY.STYLE("Currency [0]")
  129. =COLUMN.WIDTH(13,"C1")
  130. =COLUMN.WIDTH(10,"C2:C10")
  131. =FORMULA("=CROSSTAB(""Sales"",""Summary:"",values_array,TRUE,TRUE,1,TRUE)")
  132. =SELECT("R1C1")
  133. =RETURN("OK")
  134.  
  135. Table A. Excel Macro to create Cross Tab from XL Macros file.
  136. ______________________
  137. -- Zip Back script using Mark Alldritt’s 
  138. -- Script Tools scripting extensions.
  139. -- prompt for backup method
  140. display dialog "What do you want to archive?" ¬
  141.   buttons {"Files", "Folders"}
  142.  
  143. copy (button returned of the result = "Folders") ¬
  144.   to isfolders
  145.  
  146. -- get list of files/folders
  147. if isfolders then
  148.   copy (choose several folders) to flist
  149. else
  150.   copy (choose several files) to flist
  151. end if
  152.  
  153. -- get backup archive name
  154. copy (choose new file default name "backup.zip" ¬
  155.   with prompt "Save archive as:") to tmprec
  156. copy ((folder returned of tmprec as string) & ¬ 
  157.   filename returned of tmprec) to zipFileName
  158.  
  159. tell application "ZipIt"
  160.    
  161.   New window
  162.   activate "ZipIt"
  163.    
  164.   repeat with myitem in flist
  165.     Add file (myitem as string)
  166.   end repeat
  167.    
  168.   with timeout of 3600 seconds
  169.     Compress to file zipFileName
  170.   end timeout
  171.    
  172.   quit
  173.   return zipFileName
  174. end tell
  175.  
  176. ________________
  177.  
  178. * FoxPro Regions program
  179.  
  180. #DEFINE tab CHR(9)
  181. #DEFINE crlf CHR(13)+CHR(10)
  182.  
  183. m.filepath = SET("DEFAULT") + ;
  184.   "MACHETE:APPLESCRIPT:"
  185. m.salesdbf = m.filepath + "MAC SALES.DBF"
  186. m.xlscript = m.filepath + "Create Chart"
  187. m.tmpstr = " "
  188.  
  189. CLOSE DATA
  190.  
  191. USE (m.salesdbf) ALIAS macsales AGAIN
  192.  
  193. SELECT DISTINCT region FROM macsales ;
  194.   INTO ARRAY rgnarr
  195. SELECT DISTINCT YEAR(date) AS Year FROM macsales;
  196.   ORDER BY 1;
  197.   INTO ARRAY yeararr
  198.  
  199. * create string for clipboard
  200. FOR i = 1 TO ALEN(yeararr)
  201.   m.tmpstr=m.tmpstr + tab + ;
  202.     ALLTRIM(STR(yeararr(m.i)))
  203. ENDFOR
  204. m.tmpstr=m.tmpstr + crlf
  205.  
  206. FOR i = 1 TO ALEN(rgnarr)
  207.   m.tmpstr=m.tmpstr + rgnarr(m.i) +tab
  208.   FOR j = 1 TO ALEN(yeararr)
  209.     SUM unitprice*quantity TO m.tmpsum ;
  210.       FOR region = rgnarr(m.i) AND ;
  211.       YEAR(date)=yeararr(m.j)
  212.     m.tmpstr=m.tmpstr + STR(m.tmpsum) + ;
  213.       IIF(j=ALEN(yeararr),'',tab)
  214.   ENDFOR
  215.   m.tmpstr=m.tmpstr + crlf
  216. ENDFOR
  217.  
  218. _CLIPTEXT = m.tmpstr
  219. RUNSCRIPT (m.xlscript) TO retval
  220.  
  221.  
  222.  
  223. --AppleScript script 
  224. tell application "Microsoft Excel"
  225.    
  226.   -- Set up variables
  227.   set charttitle to "Annual Sales by Region"
  228.   set charttype to three D column
  229.   set chartlegend to true
  230.    
  231.   -- Now do the work
  232.   activate "Microsoft Excel"
  233.   make Document
  234.   Evaluate "Paste()" --paste contents of clipboard
  235.   Evaluate "APPLY.STYLE(\"Comma [0]\")"
  236.    
  237.   (* this repeat loop is used to convert Excel 
  238.   numbers into labels for the series headings *)
  239.   repeat with snum from 2 to 99 -- use large number
  240.     copy value of Cell snum to scontents
  241.     if scontents is equal to "" then
  242.       exit repeat
  243.     end if
  244.     try
  245.       get scontents as integer -- check for number
  246.       set the value of Cell snum to "'" & scontents
  247.       on error -- loop around
  248.     end try
  249.   end repeat
  250.    
  251.   make new Chart
  252.   set type of first Chart to charttype
  253.   set variant of first Chart to 1
  254.   set has legend of first Chart to chartlegend
  255.   set title of first Chart to charttitle
  256.    
  257.   copy first Chart to finalchart
  258.    
  259.   close first Document saving no
  260.   close first Document saving no
  261.   quit --not needed if you have enough memory
  262. end tell
  263.  
  264. (* Need to launch WP first if not already running, 
  265. else the chart gets lost. Also, make sure you have
  266. stationery file "Machete" loaded. *)
  267. run application "WordPerfect"
  268. tell application "WordPerfect"
  269.   ignoring application responses
  270.     activate "WordPerfect"
  271.     -- this is a stationery file
  272.     open (a reference to file "Machete") 
  273.     make new paragraph with data finalchart
  274.     move paragraphs 2 thru 14 to front
  275.   end ignoring
  276. end tell
  277.  
  278.  
  279.  
  280.